package com.xuecheng.content.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.query.CourseBaseQueryParams;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.impl.CourseBaseServiceImpl;
import com.xuecheng.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author 26455
 * @version 1.0
 * @description TODO
 * @date 2023/2/2 17:18
 */
@Api(value = "课程管理相关的接口", tags = "课程管理相关的接口")
@RestController
@RequestMapping("/course")
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseServiceImpl courseBaseService;

    @ApiOperation("课程查询接口")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    @PostMapping("/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody CourseBaseQueryParams queryParams) {
        //取出用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Page<CourseBase> page = new Page(pageParams.getPageNo(), pageParams.getPageSize());
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        String companyId = user.getCompanyId();
        if (StringUtils.isNotEmpty(companyId)) {
            queryWrapper.eq(CourseBase::getCompanyId, Long.parseLong(companyId));
        }
        queryWrapper.like(StringUtils.isNotEmpty(queryParams.getCourseName()), CourseBase::getName, queryParams.getCourseName());
        queryWrapper.eq(StringUtils.isNotEmpty(queryParams.getAuditStatus()), CourseBase::getAuditStatus, queryParams.getAuditStatus());
        queryWrapper.eq(StringUtils.isNotEmpty(queryParams.getPublishStatus()), CourseBase::getStatus, queryParams.getPublishStatus());

        courseBaseService.page(page, queryWrapper);

        return PageResult.success(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @ApiOperation("新增课程基础信息")
    @PostMapping()
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated AddCourseDto addCourseDto) {
        Long companyId = 1232141425L;
        CourseBaseInfoDto courseBase = courseBaseService.createCourseBase(companyId, addCourseDto);
        return courseBase;
    }

    @ApiOperation("根据课程id查询课程信息接口")
    @GetMapping("/{id}")
    public CourseBaseInfoDto QueryCourseById(@PathVariable Long id) {

        //取出当前用户身份
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        System.out.println(principal);
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user);

        return courseBaseService.getCourseBaseInfo(id);
    }

    @ApiOperation("修改课程接口")
    @PutMapping
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated EditCourseDto editCourseDto) {
        Long companyId = 1232141425L;
        return courseBaseService.updateCourseBase(companyId, editCourseDto);

    }


}
